﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace arcchallenge.filewriter.tests
{
    [TestFixture]
    public class test_TextFileWriter
    {
        [Test]
        public void Configure_with_a_path()
        {
            var config = new TextFileWriter.Configuration();
            var sut = new TextFileWriter(config);

            sut.In_Configure(@"c:\test\");

            Assert.AreEqual(@"c:\test\", config.defaultDirectoryname);
        }


        [Test]
        public void Normalize_default_path()
        {
            var config = new TextFileWriter.Configuration();
            var sut = new TextFileWriter(config);

            sut.In_Configure(@"c:\test");

            Assert.AreEqual(@"c:\test\", config.defaultDirectoryname);
        }


        [Test]
        public void Open_the_file_to_write_to()
        {
            var config = new TextFileWriter.Configuration();
            var sut = new TextFileWriter(config);

            sut.In_WriteFile(@"test.txt");

            Assert.IsNotNull(config.destination);
            config.destination.Close();
        }


        [Test]
        public void Write_file()
        {
            File.Delete("test.txt");
            var sut = new TextFileWriter();

            sut.In_WriteFile(@"test.txt");

            sut.In_WriteLine("1");
            sut.In_WriteLine("22");
            sut.In_WriteLine(null);

            Assert.AreEqual(new[] {"1", "22"}, File.ReadAllLines("test.txt"));
        }

        
        [Test]
        public void Write_file_in_default_path()
        {
            Directory.CreateDirectory("Testfiles");
            File.Delete(@"Testfiles\test.txt");

            var config = new TextFileWriter.Configuration {defaultDirectoryname = @"Testfiles\"};
            var sut = new TextFileWriter(config);

            sut.In_WriteFile(@"test.txt");

            sut.In_WriteLine("1");
            sut.In_WriteLine(null);

            Assert.AreEqual(new[] { "1" }, File.ReadAllLines(@"Testfiles\test.txt"));
        }
    }
}
